package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduTeacher;
import com.jf3q.common.service.EduCourseService;
import com.jf3q.common.service.TeacherService;
import com.jf3q.common.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    RedisTemplate redisTemplate;


    @Autowired
    TeacherService teacherService;

    @Autowired
    EduCourseService courseService;


    //首页讲师排行榜

    @GetMapping("/index")
    public ResultVo getIndexTeacher(){


        //讲师的播放量数据存储在 Redis 中(实时)

        //把所有的讲师id和讲师所有播放量放到redis的zset中排序
//        redisTemplate.opsForZSet().add("zset1","f1",80);
//        redisTemplate.opsForZSet().add("zset1","f2",70);
//        redisTemplate.opsForZSet().add("zset1","f3",90);
        System.out.println("size:"+redisTemplate.opsForZSet().size("zset1"));
        System.out.println(redisTemplate.opsForZSet().range("zset1",0,-1));
        System.out.println(redisTemplate.opsForZSet().rank("zset1","f1"));
        return null;

    }


    @GetMapping("/getTeacherFrontInfo/{teacherId}")
    public ResultVo getTeacher(@PathVariable String teacherId){
        EduTeacher teacher = teacherService.getById(teacherId);
        //根据讲师id查询这个讲师的课程列表
        List<EduCourse>courseList=courseService.selectByTeacherId(teacher);
        Map map=new HashMap();
        map.put("courseList",courseList);
        map.put("teacher",teacher);
        return ResultVo.success("成功",map);
    }



    @Operation(summary = "名师大咖")
    @GetMapping("/teachers")
    public ResultVo getHotTeacher(){
        Set zset = redisTemplate.opsForZSet().range("teacherBuyCount", 0, 3);//查排在前面四个老师
        if(zset.size()>0){//zset里有数据的时候
            LambdaQueryWrapper<EduTeacher> queryWrapper= new LambdaQueryWrapper<>();
            // 使用in函数指定要查询的ID列表
            queryWrapper.in(EduTeacher::getId, zset);
            // 执行查询
            List<EduTeacher> list = teacherService.list(queryWrapper);
            return ResultVo.success(list);
        }else{//zet里没东西的时候（还没一个人买任何老师的课）
            Page<EduTeacher> page = teacherService.page(new Page<>(1, 4));
            // 执行查询
            List<EduTeacher> list =page.getRecords();
            return ResultVo.success(list);

        }

    }


    //1 分页查询讲师的方法
    @PostMapping("/{page}/{limit}")
    public ResultVo getTeacherFrontList(@PathVariable long page, @PathVariable long limit) {
        Page<EduTeacher> pageTeacher = new Page<>(page,limit);
        Map<String,Object> map = teacherService.getTeacherFrontList(pageTeacher);
        //返回分页所有数据
        return ResultVo.success(map);
    }
}
